using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

using Tomahawk.Runtime.Rendering.Objects;
using Tomahawk.Runtime.Rendering;
using Tomahawk.Runtime.Rendering.Materials;
using Tomahawk.Runtime.Resources;

namespace Tomahawk.Runtime.Logic
{
    public class XMaterialBillboardComponent : XRenderableComponent
    {
        MaterialQuad quadModel;

        /// <summary>
        /// Constructor
        /// </summary>
        public XMaterialBillboardComponent()
            : base()
        {
            //default priority is: Scene
            this.RenderStage = Engine.RenderingStages.Scene;
        }

        #region "Serializable properties"

        /// <summary>
        /// The name of the material to be used
        /// </summary>
        [XmlElement("MaterialDefinition")]
        public SASMaterial.MaterialDefinition Material;

        protected RenderManager.Layers layer = RenderManager.Layers.Standard;

        [XmlElement("RenderLayer")]
        public RenderManager.Layers Layer { get { return layer; } set { layer = value; } }

        protected string renderGroups = "Color";

        /// <summary>
        /// Indicates the render modes this mesh belongs to
        /// </summary>
        [XmlElement("RenderGroups")]
        public string RenderGroups
        {
            get { return renderGroups; }
            set
            {
                renderGroups = value;
                if (SceneNode != null)
                {
                    SceneNode.RenderGroupNames = this.RenderGroups;
                }
            }
        }
        
        [XmlAttribute("pool")]
        public ResourceManager.ResourcePool ResourcePool = ResourceManager.ResourcePool.GameState;

        #endregion

        #region "Public properties"

        /// <summary>
        /// Returns the rendering object used to render the sprite
        /// </summary>
        public MaterialQuad QuadModel
        {
            get { return quadModel; }
        }

        #endregion

        /// <summary>
        /// Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            //create the quad model, and attach it to the scenenode
            MaterialQuad.MaterialQuadDefinition def = new MaterialQuad.MaterialQuadDefinition()
            {
                materialName = Material.MaterialName,
                diffuseMapName = Material.DiffuseMapName,
                normalMapName = Material.NormalMapName,
                specularMapName = Material.SpecularMapName,
                environmentMapName = Material.EnvironmentMapName,
                lightMapName = Material.LightMapName
            };
                
            this.quadModel = new MaterialQuad(layer, def, this.ResourcePool);
            this.SceneNode.RenderGroupNames = this.RenderGroups;
            this.SceneNode.Attach(this.quadModel);

            //set as billboard mode
            this.SceneNode.IsBillBoard = true;

        }

        protected override void OnUpdate(float elapsedTime)
        {
            base.OnUpdate(elapsedTime);
        }

        #region "Readyness"

        /// <summary>
        /// Returns true when all the resource dependant objects are ready
        /// </summary>
        protected override bool checkResourcesReady()
        {
            return base.checkResourcesReady() &&
                this.quadModel.IsReady;
        }

        #endregion

    }

}
